#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import socket


class Client():
    """Documentation for ClassName

    """

    def __init__(self, cl, path, f):
        self.cl = socket.socket(cl)
        self.path = path
        self.sf = f

    def run(self):
        fd = os.path.join(self.path, self.sf)
        if os.path.isfile(fd):
            self.sendfile(fd)
        elif os.path.exists(fd):
            self.senddir(fd)

    def senddir(self, f):
        pass

    def sendfile(self, f):

        pass


def main():
    if len(sys.argv) < 2:
        print("请选择共享目录")
        return
    path = sys.argv[1]
    if not os.path.exists(path):
        print("%s不存在" % path)
        return
    print("Start...")
    isfile = lambda f: "f:" if os.path.isfile(os.path.join(path, f)) else "d:"
    fds = [isfile(item) + item for item in os.listdir(path)]
    fdstr = "\r\n"
    fdstr = fdstr.join(fds)
    svr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    svr.bind(('', 7777))
    svr.listen(10)
    print("listen port 7777")
    while True:
        client, addr = svr.accept()
        print("ip: %s connection" % str(addr))
        # client.send(fdstr.encode('utf-8'))
        data = client.recv(2048)
        clientf = data.decode('utf-8')
        cl = Client(client, path, clientf)
        cl.run()
if __name__ == "__main__":
    main()
